
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>3.画三角形</title>
	<style>
		canvas {
            background-color: rebeccapurple;
		}
	</style>
</head>
<body>
	<canvas id="gl" style="background-color: rebeccapurple;"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		attribute vec4 a_position;
		void main() {
			gl_Position = a_position;
		}
	</script>
	
	<script type="x-shader/x-fragment" id="fragment">
		void main(){
			gl_FragColor =vec4(0.0,1.0,1.0,1.0);
		}
	</script>
	<script>
		 /** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("gl")
		const gl=canvas.getContext("webgl")
		canvas.width=1000
		canvas.height=1000
		//流程 1.创建着色器 编译 链接
		const vertex=document.getElementById("vertex").textContent
		const fragment=document.getElementById("fragment").textContent

		const vertexShader=gl.createShader(gl.VERTEX_SHADER)
		const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)
		gl.shaderSource(vertexShader,vertex)
		gl.shaderSource(fragmentShader,fragment)
		gl.compileShader(vertexShader)
		gl.compileShader(fragmentShader)
		const program=gl.createProgram()
		gl.attachShader(program,vertexShader)
		gl.attachShader(program,fragmentShader)
		gl.linkProgram(program)
		gl.useProgram(program)


		const a_position=gl.getAttribLocation(program,"a_position")
		const bufferData=gl.createBuffer()
		gl.bindBuffer(gl.ARRAY_BUFFER,bufferData)
		gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([
			-0.5,0,
			0.5,0,
			0,-0.5
		]),gl.STATIC_DRAW)
		gl.enableVertexAttribArray(a_position)
		
		gl.vertexAttribPointer(a_position,2,gl.FLOAT,false,0,0)

		gl.viewport(0,0,1000,1000)
        gl.drawArrays(gl.TRIANGLES, 0, 3);
		//画线
	</script>
</body>
</html>